---正则表达式
local function test_rexp()
    -- local s1 = "用肩膀把目标撞开{text:[<SkillLv>/2];level:1-3}，如果遇到障碍物将会对自己造成{color:ff0000;text:[<Attr_DC>*0.1]~[<Attr_MC>*2+1];level:4-6}的伤害。"
    -- local wordList = {}
    -- for word in string.gmatch(s1, "[^{}]+") do
    --     -- print(word)
    --     local wordObj = {
    --         text = string.match(word, "text:([^;]+)") or word,
    --         color = string.match(word, "color:([^;]+)"),
    --         level = string.match(word, "level:([^;]+)"),
    --     }
    --     print(wordObj.text, " | ", wordObj.color, " | ", wordObj.level, ";")
    --     wordList[#wordList + 1] = wordObj
    -- end

    -- local s1 = "<Attr_DC>*0.1"
    -- local s2 = string.gsub(s1, "<.*>", function(s)
    --     print("-=>Test: string.gsub in ", s)
    --     return "999"
    -- end)

    -- local s3 = load("return " .. s2)()

    -- print(s3)

    -- local outStr = nil
    -- -- local s1 = "<Attr_DC>*0.1"
    -- local s1 = "<啦啦啦-aaaa><dsfaa>"
    -- -- local s2 = string.match(s1, "<(.-)>")
    -- -- outStr = s2
    -- local s2 = string.gsub(s1, "<(.*)>", function(reps)
    --     outStr = reps
    -- end)
    -- print(outStr)

    -- local s = "Et_SkillId:1001;Et_SkillLv:2;Et_Dir:7"
    -- local s = "Et_SkillId:1001;Et_SkillLv:2"

    -- local skillIDStr = string.match(s, "Et_SkillId:([^;]+)")
    -- print(skillIDStr)
    -- local skillLevelStr = string.match(s, "Et_SkillLv:([^;]+)")
    -- print(skillLevelStr)
    -- local dirStr = string.match(s, "Et_Dir:([^;]+)")
    -- print(dirStr)

    local s = "啦啦啦abda"
    -- local s = "abscd \n a啊啊啊\r \t"
    -- local s = "abscd \n ass\r \t"

    local s1, num = string.gsub(s, "%s", "")
    print(s1 .. "END")
    print("find num=", num)
end

---测试字符串连接效率
local function test_contact()
    local function stringContact()
        local s1 = "a12345678901234567890123456789"
        local s2 = "b12345678901234567890123456789"
        local s3 = "c12345678901234567890123456789"
        local sLast = s1 .. "_" .. s2 .. "_" .. s3
        return sLast
    end

    local function tableContact()
        local tb = {
            "a12345678901234567890123456789",
            "b12345678901234567890123456789",
            "c12345678901234567890123456789"
        }
        local sLast = table.concat(tb, "_")
        return sLast
    end

    local t1 = os.clock()

    for i = 1, 1000000 do
        -- stringContact()
        tableContact()
    end

    local t2 = os.clock()
    print(t2 - t1)
end

-- local function test_compareVersion()
--     local ver1 = "1.0.0.1"
--     local ver2 = "1.0.0.2"

--     local verArr1 = string.unpack(ver1, ".")
--     local verArr2 = string.unpack(ver2, ".")

--     local flag = 0
--     for k, v in pairs(verArr1) do
--         if tonumber(v) ~= tonumber(verArr2[k]) then
--             flag = tonumber(v) - tonumber(verArr2[k])
--             break
--         end
--     end
    
--     print(flag)
-- end

local function Utf8to32(utf8str)
	assert(type(utf8str) == "string")
	local res, seq, val = {}, 0, nil
	for i = 1, #utf8str do
		local c = string.byte(utf8str, i)
		if seq == 0 then
			table.insert(res, val)
			seq = c < 0x80 and 1 or c < 0xE0 and 2 or c < 0xF0 and 3 or
			      c < 0xF8 and 4 or --c < 0xFC and 5 or c < 0xFE and 6 or
				  error("invalid UTF-8 character sequence")
			val = bit32.band(c, 2^(8-seq) - 1)
		else
			val = bit32.bor(bit32.lshift(val, 6), bit32.band(c, 0x3F))
		end
		seq = seq - 1
	end
	table.insert(res, val)
	table.insert(res, 0)
	return res
end

---     传入"4E00"，0x4E00 用二进制表示为 0100 1110 0000 0000，UTF-8码就是 1110[0100] 10[1110][00 (10)00][0000]，传出{228, 184, 128}
---     传入"9FA5"，0x4E00 用二进制表示为 1001 1111 1010 0101，UTF-8码就是 1110[1001] 10[1111][10 (10)10][0101]，传出{233, 190, 165}
local function test_onlychinese(s)
    -- local res = string.match(s, "[u4e00-u9fa5]")
    -- local res = string.match(s, "[%z\1-\127\194-\244][\128-\191]+")
    -- local res = string.match(s, "^[\0x4e00-\0x9fa5]+$")
    local res = string.match(s, "^[一-龥]+$")
    -- local res = string.match(s, "^[一-龥]")
    print(res)
    return res == s

    -- print("----")
    -- print(0x4e00)
    -- print(0x9fa5)
    -- print("----")
    -- print(0x2E80)
    -- print(0x9FFF)
    -- print("----")
    -- print(string.byte("一"))
    -- print(string.byte("龥"))
    -- local chineseMin = {228, 184, 128}
    -- local chineseMax = {233, 190, 165}
    -- local len = #s
    -- local i = 1
    -- while i <= len do
    --     local byteArr = {string.byte(s, i, i + 3)}
    --     local res = byteArr[1]*512 + byteArr[2]*64 + byteArr[3]
    --     print(res)
    --     -- if res < 0x4e00 or res > 0x9fa5 then
    --     if res < 145408 or res > 310597 then
    --         return false
    --     end

    --     i = i + 3
    -- end

    -- return true
end

local function test_json()
    local t1 = {
        a = "a",
        bb = 2
    }

    -- print(json.)
end

return function()
    print("===>Test: lua_string")
    -- test_rexp()
    -- test_contact()
    -- test_compareVersion()

    -- local s = "啥二一个中点文龤字啊啊啊符"
    -- -- if string.match(s, "^[一-龥]+$") == s then -- [5.1 ~ 5.4]
    -- if string.match(s, "^[\228\184\128-\233\190\165]+$") == s then -- [5.1 ~ 5.4]
    -- -- if string.match(s, "^[\u{4e00}-\u{9fa5}]+$") == s then -- 5.4
    --     print("ok")
    -- else
    --     print("not")
    -- end

    -- local s = "<啊啊啊Text|#=1|x=1>"
    -- local res = string.find(s, "^<%a+|")
    -- print(res)

    test_json()
end
